﻿using System.Data.Entity.ModelConfiguration;
using yiCommerce.Core.Domain.Addresses;
using yiCommerce.Core.Domain.Customers;

namespace yiCommerce.Data.Mapping.Customers
{
    public class CustomerMapping : EntityTypeConfiguration<Customer>
    {
        public CustomerMapping()
        {
            this.ToTable("Customer");
            this.HasKey(c => c.Id);
            this.Property(u => u.Username).HasMaxLength(1000);
            this.Property(u => u.Email).HasMaxLength(1000);
            this.Property(u => u.Password);

            this.HasOptional(b => b.Shop)
             .WithMany()
             .HasForeignKey(b => b.ShopId).WillCascadeOnDelete(false);

            this.HasOptional(b => b.CustomerLogo).WithMany()
             .HasForeignKey(b => b.CustomerLogoId).WillCascadeOnDelete(false);

            this.HasMany<Address>(c => c.Addresses)
                           .WithMany()
                           .Map(m => m.ToTable("CustomerAddresses"));

            this.HasMany(c => c.CustomerRoles)
            .WithMany()
            .Map(m => m.ToTable("Customer_CustomerRole_Mapping"));

            this.HasOptional<Address>(c => c.ShippingAddress);


            this.HasOptional(b => b.FriendShipGroup).WithMany(t => t.Members).HasForeignKey(b => b.FriendShipGroupId).WillCascadeOnDelete(false);

        }
    }
}